﻿Imports FSUIPC
Module Altitude_Knots

#Region "FSUIPC Addresses Screen 1 - Knots/Alt."
    Dim Airspeed_IAS As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen1", &H2BC)
    Dim Airspeed_TAS As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen1", &H2B8)
    Dim Airspeed_MACH As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen1", &H11C6)
    Dim Alt_Vetical As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen1", &H2C8)
    Dim Altitude As Offset(Of Integer) = New FSUIPC.Offset(Of Integer)("Screen1", &H3324)
    Dim Heading As Offset(Of Double) = New FSUIPC.Offset(Of Double)("Screen1", &H2CC)
#End Region

#Region "Screen 1 - Speed / Altitude."
    Public Sub Display_Screen()
        Dim img As New System.Drawing.Bitmap(160, 43)
        Dim screen As Drawing.Graphics = Graphics.FromImage(img)
        Try
            FSUIPC.FSUIPCConnection.Process("Screen1") ' Read Knots/Altitude Addresses.

            Dim Airspeed_IAS_Calc As Double = (Airspeed_IAS.Value / 128D)
            Dim Airspeed_TAS_Calc As Double = (Airspeed_TAS.Value / 128D)
            Dim Airspeed_MACH_Calc As Double = (Airspeed_MACH.Value / 20480)
            Dim Alt_Vertical_Calc As Double = (Alt_Vetical.Value * 60 * 3.2804 / 256)
            Dim Altitude_Calc As Integer = (Altitude.Value)
            Dim Altitude_String As String = Altitude_Calc.ToString

            ' Screen Layout.
            ' Left Side.
            screen.DrawString("Alt:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(6, 2, 160, 43))
            screen.DrawString("Ft", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(62, 2, 160, 43))

            screen.DrawString("VS:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(5, 14, 160, 43))
            screen.DrawString("Ft", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(62, 14, 160, 43))

            screen.DrawString("Hdg:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(0, 26, 160, 43))
            screen.DrawString("Deg", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(55, 26, 160, 43))

            ' Right Side.
            screen.DrawString("IAS:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(86, 2, 160, 43))
            screen.DrawString("Kts", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(141, 2, 160, 43))

            screen.DrawString("TAS:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(85, 14, 160, 43))
            screen.DrawString("Kts", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(141, 14, 160, 43))

            screen.DrawString("Mach:", New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(80, 26, 160, 43))

            screen.DrawLine(Pens.White, 0, 0, 159, 0)
            screen.DrawLine(Pens.White, 0, 37, 159, 37)
            screen.DrawLine(Pens.White, 79, 0, 79, 37)
            screen.DrawLine(Pens.White, 0, 0, 0, 37)
            screen.DrawLine(Pens.White, 159, 0, 159, 37)

            ' Display Data.
            screen.DrawString(FormatNumber(Airspeed_IAS_Calc, 1), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(110, 2, 160, 43))
            screen.DrawString(FormatNumber(Alt_Vertical_Calc, 0), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(24, 14, 160, 43))
            screen.DrawString(FormatNumber(Heading.Value, 1), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(24, 26, 160, 43))
            screen.DrawString(FormatNumber(Airspeed_TAS_Calc, 1), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(110, 14, 160, 43))
            screen.DrawString(FormatNumber(Airspeed_MACH_Calc, 3), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(110, 26, 160, 43))

            ' If Altitude is 6000 or more altitude transition, change to Flight Levels.
            If Altitude_Calc <= 5999 Then
                screen.DrawString(FormatNumber(Altitude_Calc, 0), New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(24, 2, 160, 43))
            ElseIf Altitude_Calc <= 9999 Then
                Altitude_String = Altitude_String.Substring(0, 2)
                screen.DrawString("FL0" & Altitude_String, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(24, 2, 160, 43))
            ElseIf Altitude_Calc >= 10000 Then
                Altitude_String = Altitude_String.Substring(0, 3)
                screen.DrawString("FL" & Altitude_String, New Font(FontType, FontSize, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.White, New RectangleF(24, 2, 160, 43))
            End If

        Catch exFSUIPC As FSUIPCException
            MainTimer.Stop()
            CheckTimer.Start()
        Finally
            G15FS9_Main.lcd.DrawLCD(img)
            G15FS9_Main.lcd.UpdateLCD()
        End Try
    End Sub
#End Region

End Module
